package org.epidroid.epidroid.androsoul.ui.conversation;

import java.util.Iterator;

import org.epidroid.epidroid.R;
import org.epidroid.epidroid.androsoul.engine.AndroSoul;
import org.epidroid.epidroid.androsoul.engine.contacts.NetsoulContact;
import org.epidroid.epidroid.androsoul.engine.conversation.Conversation;
import org.epidroid.epidroid.androsoul.engine.conversation.OpenedConversations;
import org.epidroid.epidroid.androsoul.engine.service.IAndroSoulService;
import org.epidroid.epidroid.androsoul.ui.AndroSoulActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

/*
 * Display the conversation switcher using the list of opened
 * conversations given by androsoul
 */
public class ConversationsSwitcher {

	private IAndroSoulService _service = null;
	private AndroSoulActivity _activity = null;
	private HorizontalScrollView _scroll = null;
	private TableRow _row = null;

	OpenedConversations _openedConversations = null;

	public ConversationsSwitcher(IAndroSoulService service,
			AndroSoulActivity activity, TableRow row,
			HorizontalScrollView scroll) {
		_service = service;
		_row = row;
		_activity = activity;
		_scroll = scroll;
		_openedConversations = _service.getOpenedConversations();

		update();
	}

	public void hide() {
		_row.removeAllViews();
	}

	// TODO: sort by time
	public void update() {

		Log.d(AndroSoul.LOGCAT, "updating the conversation switcher");

		if (_row == null)
			return;
		if (_openedConversations.getList() == null)
			return;

		_scroll.post(new Runnable() {
			@Override
			public void run() {
				_row.removeAllViews();
				synchronized (_openedConversations.getList()) {
					for (Iterator<Conversation> it = _openedConversations
							.getList().iterator(); it.hasNext();) {
						final Conversation item = (Conversation) it.next();

						LayoutInflater inflater = _activity.getLayoutInflater();
						View layout = inflater.inflate(
								R.layout.androsoul_conversation_switcher_item,
								null, true);

						ImageView img = (ImageView) layout
								.findViewById(R.id.androsoul_conversation_switcher_imgcontact);
						TextView txt = (TextView) layout
								.findViewById(R.id.androsoul_conversation_switcher_text);

						Drawable tmp = item.getContact().getImage(_activity,
								false);
						if (tmp != null)
							img.setImageDrawable(tmp);
						else
							img.setImageResource(R.drawable.contact_picture_2);

						int nbMsg = item.getNbUnread();
						if (nbMsg == 0)
							txt.setText(item.getContact().getName());
						else
							txt.setText(item.getContact().getName() + " ("
									+ nbMsg + ")");

						/*
						 * TODO: do that using xml
						 */
						if (nbMsg > 0)
							txt.setTextColor(Color.rgb(255, 15, 15));

						layout.setOnClickListener(new OnClickListener() {
							public void onClick(View v) {
								_activity.launchConversation(item.getContact());
							}
						});
						layout.setOnLongClickListener(new OnLongClickListener() {
							public boolean onLongClick(View v) {
								showDialogCloseConv(v, item.getContact(),
										_service);
								return false;
							}
						});

						_row.addView(layout);
						_scroll.fullScroll(View.FOCUS_LEFT);
					}
				}
			}
		});

		int length = _openedConversations.getList().size();

		Log.d(AndroSoul.LOGCAT, "the conversation switcher has " + length
				+ " items");

		if (length == 0) {
			_scroll.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, 0));
		} else {
			int value = (int) TypedValue.applyDimension(
					TypedValue.COMPLEX_UNIT_DIP, (float) 35, _activity
							.getResources().getDisplayMetrics());
			_scroll.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, value));
		}

	}

	private void showDialogCloseConv(View v, final NetsoulContact contact,
			final IAndroSoulService service) {
		AlertDialog.Builder alert = new AlertDialog.Builder(_activity);
		alert.setTitle(_activity.getString(R.string.conversation_with) + " " + contact.getName());

		alert.setPositiveButton(R.string.cancel, null);
		alert.setNeutralButton(R.string.conversation_close,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						service.closeConversation(contact);
						update();
					}
				});
		alert.show();
	}

}
